﻿using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TelecoSystems.Web.Models;
using TelecoSystems.Web.Services.Products;
using TelecoSystems.Web;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

namespace TelecoSystems.Tests
{
    [TestClass]    
    public class ProductsTests : SilverlightTest
    {
        ProductsDomainContext productsCtx;

        [TestInitialize]
        public void Initialize()
        {
            productsCtx = new ProductsDomainContext();
        }

        [TestMethod]
        [Asynchronous]    
        [Description("Tests data retrieval from products service.")]
        public void TestGetProducts()
        {
            //LoginOperation loginOperation;
            //bool loginCompleted = false;           

            //EnqueueCallback(() => {               
            //    loginOperation = WebContext.Current.Authentication.Login("manager", "password*");
            //    loginOperation.Completed += (s, e) =>
            //    {
            //        loginCompleted = true;
            //    };
            //});            

            //EnqueueConditional(() => loginCompleted);            

            //bool isAuth = WebContext.Current.User.IsAuthenticated;

            LoadOperation<Product> loadOp = productsCtx.Load<Product>(productsCtx.GetProducsQuery(), (lo) =>
            {
                lo.Completed += (s, e) =>
                {
                    Assert.IsFalse(lo.HasError, "LoadOperation shouldn't raise error.");

                    ObservableCollection<Product> products = new ObservableCollection<Product>();
                    foreach (Product p in lo.Entities) products.Add(p);

                    Assert.IsTrue(products.Count > 0, "There should be at least 1 product loaded.");
                    CollectionAssert.AllItemsAreInstancesOfType(products, typeof(Product), "All items should be instances of type Product.");
                };
            }, null);

            //EnqueueConditional(() => loadOp.IsComplete == true);
            //Assert.IsFalse(loadOp.HasError);

            //ObservableCollection<Product> products = new ObservableCollection<Product>();
            //foreach (Product p in loadOp.Entities) products.Add(p);

            //CollectionAssert.AllItemsAreInstancesOfType(products, typeof(Product));
            
            //EnqueueTestComplete();
            TestComplete();
        }
    }
}